/**
 * getGradeList   获取年级列表
 * addGrade       添加年级
 * getInfoGrade   获取年级信息
 * updateGrade    修改年级信息
 * deleteGrade    删除年级
 */
const gradeStatement = require("../../common/sql_sentences/grade")
const classStatement = require("../../common/sql_sentences/class")

exports.getGradeList = async (req, res) => {

  try {
    if (req.query.name) {
      let { page, pagesize, name } = req.query
      // 根据name模糊查询年级总数
      let total,
        sqlStr = gradeStatement.selectCountLike(name)
      let results = await RunSQL(sqlStr)
      if (!results[0].total) return res.successSend("没有找到相关名称！", 10005)
      total = results[0].total

      // 根据name模糊查询获取年级列表
      sqlStr = gradeStatement.selectLikeLimit(name, page, pagesize)
      results = await RunSQL(sqlStr)
      return res.successSend("", 10000, { row: results, total })
    } else {
      let { page, pagesize } = req.query
      // 获取年级总数
      let total,
        sqlStr = gradeStatement.selectCount()
      let results = await RunSQL(sqlStr)
      total = results[0].total

      if (!pagesize) {
        pagesize = total
      }

      // 获取年级列表
      sqlStr = gradeStatement.selectLimit(page, pagesize)
      results = await RunSQL(sqlStr)
      return res.successSend("", 10000, { row: results, total })

    }
  } catch (error) {
    res.errSend(error)
  }

}

exports.addGrade = async (req, res) => {
  try {
    const data = req.body
    // 查询年级名称是否重复
    let sqlStr = gradeStatement.select({ name: data.name })
    let results = await RunSQL(sqlStr)
    if (results[0]?.id && results[0]?.id !== data.id) return res.successSend("名称已存在！", 10005)

    // 添加年级
    sqlStr = gradeStatement.insert(req.body)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("添加年级失败！", 10005)
    return res.successSend("添加年级成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.getInfoGrade = async (req, res) => {
  try {
    // 获取年级信息
    let sqlStr = gradeStatement.idSelect(req.body.id)
    let results = await RunSQL(sqlStr)
    if (results.length !== 1)
      return res.successSend("获取年级信息失败！", 10005)
    return res.successSend("", 10000, results[0])
  } catch (error) {
    res.errSend(error)
  }
}

exports.updateGrade = async (req, res) => {
  try {
    const data = req.body
    // 查询年级名称是否重复
    let sqlStr = gradeStatement.select({ name: data.name })
    let results = await RunSQL
    if (results[0]?.id && results[0]?.id && results[0]?.id !== data.id)
      return res.errSend("名称已存在！", 10005)

    // 修改年级信息
    sqlStr = gradeStatement.idUpdate(data, data.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("修改年级信息失败！", 10005)
    return res.successSend("修改年级信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.deleteGrade = async (req, res) => {
  try {
    const data = req.params
    // 获取年级信息
    let sqlStr = gradeStatement.idSelect(data.id)
    let results = await RunSQL(sqlStr)
    if (results.length !== 1)
      return res.successSend("！", 10005)

    // 查询该学部是否存在班级
    sqlStr = classStatement.select({ faculty: results[0].name })
    results = await RunSQL(sqlStr)
    if (results.length)
      return res.successSend("删除年级失败!有班级属于该年级，请删除班级后重试！", 10005)

    // 删除年级
    sqlStr = gradeStatement.delete(req.params.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("删除年级失败！", 10005)
    return res.successSend("删除年级信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}
